#include "mainwidget.h"
#include "ui_mainwidget.h"

MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    this->resize(600,570);
    //    QPalette palette = this->palette();
    //    palette.setColor(QPalette::Background,Qt::black);
    //    this->setPalette(palette);
    this->center = QPoint(this->height()/2,this->width()/2);
    getRealTime();
    timer = new QTimer(this);
    timer->start(1000);
    connect(timer,&QTimer::timeout,this,&MainWidget::timeChanged);
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::getRealTime()
{
    QTime time= QTime::currentTime();
    hour = time.hour();
    minute =time.minute();
    second =time.second();

}

void MainWidget::showTime()
{
    //    QPainter hourPaintr(this);
    //    QPainter minPainter(this);
    //    QPainter secondPainter(this);

    QPainter painter(this);
    QPen pen;

    painter.translate(center);
    painter.save();
    pen.setWidth(2);
    painter.setPen(pen);
    painter.rotate((6*second)-180);
    painter.drawLine(0,0,0,260);

    painter.restore();
    painter.save();
    pen.setWidth(3);
    painter.setPen(pen);
    painter.rotate(6*(minute)-180);
    painter.drawLine(0,0,0,230);

    painter.restore();
    pen.setWidth(4);
    painter.setPen(pen);
    painter.rotate(30*(hour)-180);
    painter.drawLine(0,0,0,200);
}

void MainWidget::paintEvent(QPaintEvent *event)
{
    showTime();
    QWidget::paintEvent(event);
}

void MainWidget::timeChanged()
{
    second++;
    if(second>60)
    {
        second%=60;
        minute++;
        if(minute>60)
        {
            minute%=60;
            hour++;
            if(hour>12)
            {
                hour%=12;
            }
        }
    }
    update();
}
